<?php

    // +----------------------------------------------------------------------
    // | 行为控制器
    // +----------------------------------------------------------------------
    // | Copyright (c) 2015-2024 http://www.yicmf.com, All rights reserved.
    // +----------------------------------------------------------------------
    // | Author: 微尘 <yicmf@qq.com>
    // +----------------------------------------------------------------------

    namespace app\controller\ucenter\admin;

    use app\model\ucenter\Action as ActionModel;
    use app\event\ucenter\Action as ActionEvent;
    use yicmf\builder\Edit;
    use yicmf\builder\Table;
    use think\facade\Url;
	use app\controller\admin\Common;

    class Action extends Common
    {
        protected function initialize()
        {
            parent::initialize();
        }

        public function _filter()
        {
            return [];
        }

        /**
         * 行为管理
         * @return mixed|string
         * @author  : 微尘 <yicmf@qq.com>
         */
        public function index()
        {

            // 显示页面
            $builder = new Table();
            $builder->model(ActionModel::class)
                ->where($this->_filter())
                ->order('id DESC')
//				->suggest('合理搭配日志规则，方便管理网站运行')
                ->searchText('title', '操作名称', '操作名称')
                ->buttonUpdate(url('ucenter/admin/Action/update'))
                ->keyId()
                ->keyTitle('操作名称')
                ->keyText('name', '标识')
				->keyBool('is_open','开启记录')
                ->keyText('remark', '操作描述')
                ->keyText('log_rule', '日志规则')
                ->keyUpdateTime()
                ->keyStatus()
                ->actionUpdate('ucenter/admin/Action/update?id={$id}', '编辑', function($data) {
					if ($data['id'] > 3) {
						return true;
					} else {
						return false;
					}
				})
                ->actionDelete('ucenter/admin/Action/del?id={$id}','删除', function($data) {
                    if ($data['id'] > 3) {
                        return true;
                    } else {
                        return false;
                    }
                })
            ;
            return $builder->fetch();
        }

        /**
         * 更新|编辑行为
         * @return string|void
         * @throws \Exception
         * @author  : 微尘 <yicmf@qq.com>
         * @datetime: 2019/5/8 15:48
         */
        public function update()
        {
            if ($this->request->isPost()) {
                // 数据验证
                $result = $this->validate($this->request->post(), 'action');
                if ($result !== true) {
                    $this->_returnError( '行为数据验证错误，【' . $result . '】');
                } else {
                    $event = new ActionEvent();
                    $result = $event->update($this->request->post(), $this->user);
                    if (0 == $result['code']) {
                        $this->_returnSuccess( '操作成功', true);
                    } else {
                        $this->_returnError( '操作失败，失败信息【' . $result['message'] . '】');
                    }
                }
            } else {
                $id = $this->request->get('id');
                //判断是否为编辑模式
                $isEdit = $id ? true : false;
                if ($isEdit) {
                    $data = ActionModel::where('id',$id)->where('status',1)->find();
                } else {
                    $data['type'] = 1;
                }
                $builder = new Edit();
                $builder->title($isEdit ? '编辑行为' : '添加行为')
					->keyTextInline('title', '行为名称', '规则名称，方便记忆')
                    ->keyTextInline('name', '行为标识', '系统调用标识，具有唯一性')
					->keyBool('is_open','开启记录')
//                    ->keyRadio('module', '所属模块', Module::getAll())
                    ->keyTextArea('remark', '行为描述', '输入行为描述')
//                    ->keyTextArea('action_rule', '行为规则', '输入行为规则，不写则只记录日志')
                    ->keyTextArea('log_rule', '日志规则', '记录日志备注时按此规则来生成，支持[变量|函数]。目前变量有：user,time,model,record,data')
                    ->explain('注意规则合理性')
                    ->data($data)
                    ->buttonSubmit('ucenter/admin/Action/update', '保存')->buttonClose();
                return $builder->fetch();
            }
        }
    }
